package yannan.voiplock4ble.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import yannan.voiplock4ble.BuildConfig;

public class RecyclerSupportViewEmpty extends RecyclerView {
    private View emptyView;

    private AdapterDataObserver emptyObserver = new AdapterDataObserver() {


        @Override
        public void onChanged() {
            Adapter<?> adapter = getAdapter();
            if (adapter != null && emptyView != null) {
                if (adapter.getItemCount() == 0) {
                    emptyView.setVisibility(View.VISIBLE);
                    RecyclerSupportViewEmpty.this.setVisibility(View.GONE);
                } else {
                    emptyView.setVisibility(View.GONE);
                    RecyclerSupportViewEmpty.this.setVisibility(View.VISIBLE);
//                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(emptyView, "alpha", 1.0f, 0.0f);
//                    objectAnimator.start();
                }
            }

        }
    };

    public RecyclerSupportViewEmpty(Context context) {
        super(context);
    }

    public RecyclerSupportViewEmpty(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RecyclerSupportViewEmpty(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);

        if (adapter != null) {
            adapter.registerAdapterDataObserver(emptyObserver);
        }

        emptyObserver.onChanged();
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (BuildConfig.DEBUG) Log.e("RecyclerSupportViewEmpt", "onLayout");
        RecyclerView.LayoutManager layoutManager = this.getLayoutManager();
        //判断是当前layoutManager是否为LinearLayoutManager
        // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
        if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;


            //获取最后一个可见view的位置
            int lastItemPosition = linearManager.findLastVisibleItemPosition();
            //获取第一个可见view的位置
            int firstItemPosition = linearManager.findFirstVisibleItemPosition();
            System.out.println(lastItemPosition + "   " + firstItemPosition+ linearManager.findLastCompletelyVisibleItemPosition());
            if (onMenuOpenListener != null) {
                onMenuOpenListener.onMenuOpen((LinearLayoutManager) layoutManager, lastItemPosition);
            }
        }
    }

    IOnMenuOpenListener onMenuOpenListener;

    public interface IOnMenuOpenListener {
        void onMenuOpen(LinearLayoutManager linearLayoutManager, int lastPoi);
    }

    public void setOnMenuOpenListener(IOnMenuOpenListener e) {
        this.onMenuOpenListener = e;
    }
}